﻿//-----------------------------------------------------------------------
// <copyright file="DataServiceFormatInspector.cs" company="None">
// None
// </copyright>
//-----------------------------------------------------------------------
namespace WCFDataServiceFormatExtensions
{
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;

    /// <summary>
    /// Inspect incoming outgoing message and transform them in text version if required.
    /// </summary>
    public class DataServiceFormatInspector : IDispatchMessageInspector
    {
        /// <summary>
        /// This is invoked automatically by data service calls.
        /// </summary>
        /// <param name="request">Incoming request message.</param>
        /// <param name="channel">Request channel</param>
        /// <param name="instanceContext">Operation Context for this instance.</param>
        /// <returns>Returns null.</returns>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            object token = null;
            if (request.Properties.ContainsKey("UriTemplateMatchResults"))
            {
                if (!TextHandler.TextFormatRequestHandler(ref request, channel, instanceContext, out token))
                {
                    ////Pass to Next Handler - Not Implemented 
                    ////token=NextHandler()
                }
            }

            return token;
        }

        /// <summary>
        /// This is invoked automatically by data service calls.
        /// </summary>
        /// <param name="reply">Actual responce from service.</param>
        /// <param name="correlationState">Correlation token returned in “AfterRequestReceived ”function.</param>
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!TextHandler.TextFormatResponceHandler(ref reply, correlationState))
            {
                ////Pass to text handler 
            }
        }
    }
}